package com.xckj.cycling.activity

import android.content.Intent
import android.os.Bundle
import com.bumptech.glide.Glide
import com.junca.android.base.CViewBinding
import com.lihang.chart.utils.ChartLineItem
import com.xckj.cycling.R
import com.xckj.cycling.databinding.ActivityNavigationDetailBinding
import com.xckj.cycling.framework.BaseActivityKt
import com.xckj.cycling.model.NavigationDetailModel
import com.xckj.cycling.utils.setCardBg

/**
 * 运动详情
 */
class NavigationDetailActivity : BaseActivityKt() {
    @CViewBinding
    lateinit var viewBinding: ActivityNavigationDetailBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding.title.tvTitle.text = getString(R.string.sport_detail)
        viewBinding.confirm.setOnClickListener {
            startActivity(Intent(this, EditRouteActivity::class.java))
        }
        viewBinding.sport.setCardBg()

        viewModel.loadNavigationDetail()
        viewModel.navigationDetail.observe(this) {
            it?.detail?.apply {
                setData(this)
            }
        }
    }

    private fun setData(detail: NavigationDetailModel) {
        Glide.with(this).load(detail.fulllineimg).into(viewBinding.map)
        val chartA = viewBinding.highlyChart.attChar
        detail.highly?.let {
            chartA.setHoriItems(it.x?.map {
                it.toString()
            }?.let { it1 -> ArrayList(it1) })
            chartA.setItems(
                arrayListOf(
                    ChartLineItem(
                        ArrayList(it.data),
                        R.color.teal_200,
                        "",
                        false,
                        false
                    )
                )
            )
            viewBinding.highlyChart.tvAtt.text = getString(R.string.leiji_pasheng, it.climb?.replace("feet",""))
        }
        val chartH = viewBinding.heartChar
        detail.heart?.let {
            chartH.setHoriItems(it.x?.map {
                it.toString()
            }?.let { it1 -> ArrayList(it1) })
            chartH.setItems(
                arrayListOf(
                    ChartLineItem(
                        ArrayList(it.data),
                        R.color.teal_200,
                        "Climb",
                        false,
                        false
                    )
                )
            )
        }

        val chartS = viewBinding.speedChar
        detail.speed?.let {
            chartS.setHoriItems(it.x?.map {
                it.toString()
            }?.let { it1 -> ArrayList(it1) })
            chartS.setItems(
                arrayListOf(
                    ChartLineItem(
                        ArrayList(it.data),
                        R.color.teal_200,
                        "Speed",
                        false,
                        false
                    )
                )
            )
        }

        viewBinding.addr.text = detail.user_post
        viewBinding.addrEnd.text = detail.end_post
        viewBinding.createTime.text = detail.createtime
        viewBinding.detailInfo.let { _vb ->
            detail.overall?.let {
                _vb.averageSpeed.text = it.entire
                _vb.up.text = it.climb
                _vb.mili.text = it.mileage
                _vb.number.text = it.number
                _vb.totalKaluli.text = it.calories
                _vb.tvFastest.text = it.fastest
                _vb.tvAverQuancheng.text = it.entire
                _vb.fastest.text = it.fastest
                _vb.startTime.text = getString(R.string.start_time, detail.createtime)
                _vb.endTime.text = getString(R.string.end_time, detail.updatetime)
                viewBinding.info.distance.text = it.mileage
                viewBinding.info.totalKaluli.text = it.totalcalories
                viewBinding.info.time.text = detail.timelength
                viewBinding.info.totalAtt.text = it.cumulativeRise
            }


        }
    }
}